//定义段
 %{
/**********************************
task1.y
***********************************/
#include<stdio.h>
#include<stdlib.h>
#include <ctype.h>
#ifndef YYSTYPE
#define YYSTYPE double
#endif
int yylex();
extern int yyparse();
FILE* yyin;
void yyerror(const char* s);
%}

%token ADD
%token SUB
%token MUL
%token DIV
%token LEFT_PAREN
%token RIGHT_PAREN
%token UMINUS
%token NUMBER
%left ADD SUB
%left MUL DIV
%right UMINUS


%%

lines : lines expr ';' { printf("%f\n", $2); }
	| lines ';'
	|
	;

expr : expr ADD expr { $$ = $1 + $3; }
	| expr SUB expr { $$ = $1 - $3; }
	| expr MUL expr { $$ = $1 * $3; }
	| expr DIV expr { $$ = $1 / $3; }
	| LEFT_PAREN expr RIGHT_PAREN { $$ = $2; }
	| SUB expr %prec UMINUS { $$ = -$2; }
	| NUMBER
	;

%%

// programs section

int yylex()
{
    int t;
    while(1){
        t = getchar();
        if(t == ' ' || t == '\t' || t == '\n'){
            //do nothing
        }
        else if(isdigit(t)){
            yylval = 0;
            while(isdigit(t)){
                yylval = yylval * 10 + t - '0';
                t = getchar();
            }
            ungetc(t,stdin);
            return NUMBER;
        }
        else{
            switch(t){
    case '+':
        return ADD;
        break;
    case '-':
        return SUB;
        break;
    case '*':
        return MUL;
        break;
    case '/':
        return DIV;
        break;
    case '(':
        return LEFT_PAREN;
        break;
    case ')':
        return RIGHT_PAREN;
    default:
        return t;
        break;
   }
        }
    }
}


int main(void)
{
    yyin = stdin ;
    do {
        yyparse();
        } 
    while (! feof (yyin));
    return 0;
}
void yyerror(const char* s) {
    fprintf (stderr , "Parse error : %s\n", s );
    exit (1);
}
